package com.climbcloud.ssm;

import com.climbcloud.ssm.entity.User;
import com.climbcloud.ssm.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

/**
 * @description:
 * @author: Administrator
 * @time: 2021/5/31 6:12
 */
public class Apitest {
    InputStream in;
    SqlSessionFactory factory;
    SqlSession session;
    @Before
    public void setup() throws IOException {
        /*读取配置文件*/
        /*法一*/
        Properties properties=new Properties();
        in = MybatisApiTest.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");
        /* *//*法二*//*
        in= Resources.getResourceAsStream("SqlMapConfig.xml");*/
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        factory=builder.build(in);
        session=factory.openSession();



    }
    @After
    public void teardown() throws IOException {
        in.close();
        session.close();
    }
    @Test
    public void testFindONE(){
        User user=session.selectOne("com.climbcloud.ssm.mapper.UserMapper.findById");
        System.out.println(user);
    }

    @Test
    public  void testFindAll(){
        List<User> users=session.selectList("com.climbcloud.ssm.mapper.UserMapper.findAll");
        for (User user:users
             ) {
            System.out.println(user);
        }
    }
}
